package com.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.bean.ArticleContext;
import com.bean.CollectArticle;
import com.bean.CollectObj;
import com.bean.CollectVideo;
import com.bean.CollectWeibo;
import com.bean.VideoContext;
import com.bean.WeiboContext;
import com.dao.CollectMessageDao;
import com.mongodb.BasicDBObject;
import com.util.DateUtil;
import com.util.LoggerUtil;
@Component
public class CollectMessageDaoImpl implements CollectMessageDao{
	public static Logger logger=Logger.getLogger(CollectMessageDaoImpl.class);
	@Autowired
	private MongoTemplate mongoTemplate;
	public Map<String, Object> insertWeibo(String userId,String oldId,JSONObject logs){
		Map<String, Object> map=new HashMap<>();
		try {
			Query query = new Query(Criteria.where("collectInfo.oldId").is(oldId).and("userId").is(userId));
			long count = mongoTemplate.count(query, CollectWeibo.class);
			if (count > 0) {
				Update update = new Update();
				update.pull("collectInfo",new BasicDBObject("oldId",oldId));
				mongoTemplate.updateFirst(query, update, CollectWeibo.class);
				map.put("code", "107");
			} else {
				Query query2=new Query(Criteria.where("userId").is(userId));
				long count2 = mongoTemplate.count(query2,CollectWeibo.class);
				CollectObj collectObj = new CollectObj();
				collectObj.setCreateDate(DateUtil.formatDateToStrAll(new Date()));
				collectObj.setOldId(oldId);
			
				if (count2 > 0) {
					Update update=new Update();
					update.addToSet("collectInfo",collectObj);
					mongoTemplate.updateFirst(query2, update, CollectWeibo.class);
				} else {
					ArrayList<CollectObj> collectObjs = new ArrayList<>();
					collectObjs.add(collectObj);
					CollectWeibo collectWeibo = new CollectWeibo();
					collectWeibo.setUserId(userId);
					collectWeibo.setCollectInfo(collectObjs);
					mongoTemplate.insert(collectWeibo);
				}
				map.put("code", "0");
			}
			LoggerUtil.backlogsSuccess(logs, logger);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		return map;
	}
	
	public Map<String, Object> insertVideo(String userId,String oldId,JSONObject logs){
		Map<String, Object> map=new HashMap<>();
		try {
			Query query = new Query(Criteria.where("collectInfo.oldId").is(oldId).and("userId").is(userId));
			long count = mongoTemplate.count(query, CollectVideo.class);
			if (count > 0) {
				Update update = new Update();
				update.pull("collectInfo",new BasicDBObject("oldId",oldId));
				mongoTemplate.updateFirst(query, update, CollectVideo.class);
				map.put("code", "107");
			} else {
				Query query2=new Query(Criteria.where("userId").is(userId));
				long count2 = mongoTemplate.count(query2,CollectVideo.class);
				CollectObj collectObj = new CollectObj();
				collectObj.setCreateDate(DateUtil.formatDateToStrAll(new Date()));
				collectObj.setOldId(oldId);
				if (count2 > 0) {
					Update update=new Update();
					update.addToSet("collectInfo",collectObj);
					mongoTemplate.updateFirst(query2, update, CollectVideo.class);
				} else {
					ArrayList<CollectObj> collectObjs = new ArrayList<>();
					collectObjs.add(collectObj);
					CollectVideo collectVideo = new CollectVideo();
					collectVideo.setUserId(userId);
					collectVideo.setCollectInfo(collectObjs);
					mongoTemplate.insert(collectVideo);
				}
				map.put("code", "0");
			}
			LoggerUtil.backlogsSuccess(logs, logger);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		return map;
	}
	public Map<String, Object> insertArticle(String userId,String oldId,JSONObject logs){
		Map<String, Object> map=new HashMap<>();
		try {
			Query query = new Query(Criteria.where("collectInfo.oldId").is(oldId).and("userId").is(userId));
			long count = mongoTemplate.count(query, CollectArticle.class);
			if (count > 0) {
				Update update = new Update();
				update.pull("collectInfo",new BasicDBObject("oldId",oldId));
				mongoTemplate.updateFirst(query, update, CollectArticle.class);
				map.put("code", "107");
			} else {
				Query query2=new Query(Criteria.where("userId").is(userId));
				long count2 = mongoTemplate.count(query2,CollectArticle.class);
				CollectObj collectObj = new CollectObj();
				collectObj.setCreateDate(DateUtil.formatDateToStrAll(new Date()));
				collectObj.setOldId(oldId);
				if (count2 > 0) {
					Update update=new Update();
					update.addToSet("collectInfo",collectObj);
					mongoTemplate.updateFirst(query2, update, CollectArticle.class);
				} else {
					ArrayList<CollectObj> collectObjs = new ArrayList<>();
					collectObjs.add(collectObj);
					CollectArticle collectArticle = new CollectArticle();
					collectArticle.setUserId(userId);
					collectArticle.setCollectInfo(collectObjs);
					mongoTemplate.insert(collectArticle);
				}
				map.put("code", "0");
			}
			LoggerUtil.backlogsSuccess(logs, logger);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		return map;
	}
	public List<WeiboContext> selectListCollectWeibo(String userId){
		List<WeiboContext> lists=new ArrayList<>();
		
		CollectWeibo collectWeibos=mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), CollectWeibo.class);
		if(collectWeibos!=null){
			ArrayList<CollectObj> objs=collectWeibos.getCollectInfo();
			Collections.reverse(objs);
			for(CollectObj obj:objs){
				WeiboContext weiboContext=mongoTemplate.findById(obj.getOldId(), WeiboContext.class);
				if(weiboContext!=null){
					lists.add(weiboContext);
				}
			}
			
		}
		return lists;
	}
	
	public List<ArticleContext> selectListCollectArticle(String userId){
		List<ArticleContext> lists=new ArrayList<>();
		
		CollectArticle collectArticle=mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), CollectArticle.class);
		if(collectArticle!=null){
			ArrayList<CollectObj> objs=collectArticle.getCollectInfo();
			Collections.reverse(objs);
			for(CollectObj obj:objs){
				ArticleContext articleContext=mongoTemplate.findById(obj.getOldId(), ArticleContext.class);
				if(articleContext!=null){
					lists.add(articleContext);
				}
			}
			
		}
		return lists;
	}
	public List<VideoContext> selectListCollectVideo(String userId){
		List<VideoContext> lists=new ArrayList<>();
		
		CollectVideo collectVideo=mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), CollectVideo.class);
		if(collectVideo!=null){
			ArrayList<CollectObj> objs=collectVideo.getCollectInfo();
			Collections.reverse(objs);
			for(CollectObj obj:objs){
				VideoContext videoContext=mongoTemplate.findById(obj.getOldId(), VideoContext.class);
				if(videoContext!=null){
					lists.add(videoContext);
				}
			}
		}
		return lists;
	}
}
